﻿////入库单明细操作
function addNewRow() {
    //选择产品信息 开始
    if ($("#BillState").val() == 1) { $.ligerDialog.success('已提交，不能添加！'); return; }
    var orgCode = $("#HidOrgCode").val();
    var returnValue = window.showModalDialog('../Product/SelectProducts?orgCode=' + orgCode, window, "help:No;resizable:No;status:No;scroll:yes;location:No;dialogWidth=800px;dialogHeight=500px");
    if (returnValue != null && returnValue != undefined) {

        var str = eval(returnValue);
        //选择产品信息 结束
        //----------------------------
        var tempId = $("#HidEmptyGuid").val();
        var billId = $("#Id").val();
        var row = manager.getSelectedRow();
        var allRows = manager.rows;
        var allLen = allRows.length;
        var tag = false;
        //参数1:rowdata(非必填)
        //参数2:插入的位置 Row Data 
        //参数3:之前或者之后(非必填)
        //----------------------------
        for (var i = 0; i < str.length; i++) {
            tag = false;
            var s = str[i].split(",");
            //********************************

            for (var k = 0; k < allLen; k++) {
                if (allRows[k]["StockID"] == s[0]) {
                    tag = true;
                    break;
                }
            }
            //********************************
            if (!tag) {
                //------------------------------------------------
                manager.addRow({
                    ID: tempId,
                    ProductInBillID: tempId,
                    ProductID: s[0],
                    TypeName: s[2],
                    ProductName: s[1],
                    ProductCode: s[3],
                    ProductStandard: s[4],
                    InlCount: 0,
                    ShelfNumber: '',
                    Remarks: ''
                }, row, document.getElementById("chkbefore"));
                //------------------------------------------------
            }
        }
    }
}

var columns =
            [
                { display: 'Key', name: 'ID', type: 'text', hide: 1 },
                { display: 'Key', name: 'ProductInBillID', type: 'text', hide: 1 },
                { display: 'ProductID', name: 'ProductID', type: 'text', hide: 1 },
                 { display: '产品类别', name: 'TypeName', mintWidth: 80, width: 120, type: 'text' },
                 { display: '产品名称', name: 'ProductName', mintWidth: 80, width: 160, type: 'text' },
                 { display: '产品编码', name: 'ProductCode', mintWidth: 80, width: 100, type: 'text' },
                { display: '产品规格', name: 'ProductStandard', mintWidth: 80, width: 200, type: 'text' },
                { display: '入库数量', name: 'InlCount', mintWidth: 60, width: 80, type: 'int', editor: { type: 'int'} },
                { display: '货架号', name: 'ShelfNumber', mintWidth: 100, width: 140, editor: { type: 'text'} },
                { display: '备注', name: 'Remarks', mintWidth: 80, width: 140, editor: { type: 'text'} }
             ];

var manager;
//init controls
$(document).ready(function () {
    intiToobar();
    initligerGrid();
    //$("#btnSubmit").unbind("click").bind("click", confirmOperation); //提交
});

function initligerGrid() {
    manager = null;
    manager = $("#ProductInBillItemsGrid").ligerGrid({
        columns: columns,
      //  title: '入库单明细',//不能要标题，修改的BUG，不在ligerui.min.js文件中，所以这个BUG还是存在
        showTitle: false,
        width: '100%',
        enabledSort: true,
        enabledEdit: true,
       // height: '80%',
        checkbox: false,
        rownumbers: true,
        toolbar: { items: [
                                   // { text: '入库单明细' },
                                    { text: '增加', click: addNewRow, icon: 'add' },
                                    { line: true },
//                                    { text: '修改', click: modifyOperation, icon: 'modify' },
//                                    { line: true },
                                    { text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' }
                                    ]
        },
         isChecked: f_isChecked, onCheckRow: f_onCheckRow, onCheckAllRow: f_onCheckAllRow,
        url: "../ProductIn/GetJsonOfProductInBillDetails?billId="+$("#hidbillId").val()+"&rand=" + Math.random()
    });
}
//add
function dialogAdd() {
    $.ligerDialog.open({ url: '../ProductIn/AddItem', height: getDialogHeight()+100, width: getDialogWidth(),
        isResize: true, isHidden: false
    });
}


//delete
function deleteOperation() {

    var row = manager.getSelectedRow();
    var ids = checkedCustomer.join(',');
    var billId = $("#hidbillId").val();
    var detailId = (row == undefined || row == null) ? null : row["ID"];
    var confirmState = $("#BillState").val();
    if (row != null && row != undefined && detailId != null && $.trim(detailId.toString()).length > 0) {
        ids = detailId.toString() + ",";
        $.ligerDialog.confirm('确定要删除吗？', function (yes) {
            if (yes) {
                if (confirmState == 0) {
                    manager.deleteSelectedRow();
                    if ($("#HidEmptyGuid").val() + "," != ids) {//删除非新增的
                        $.get("../ProductIn/Delete?billId=" + billId + "&ids=" + ids, {}, function (data) {
                            if (data != undefined && data == "9") {
                                initligerGrid();
                            }
                        });
                    }
                }
                else {
                    $.ligerDialog.success('已提交，不能删除！'); return;
                }

            }
        });
    }
    else {
        $.ligerDialog.success('请选择需要删除行！'); return;
    }
}
//update 
//function modifyOperation() {
//    var row = manager.getSelectedRow();
//    if (!row) {
//        $.ligerDialog.success('请选择要编辑的行!'); return;
//    }
//    $.ligerDialog.open({ url: '../ProductIn/Edit?id=' + row["ID"].toString(), height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
//    });
//}

//update 
//function viewOperation() {
//    var row = manager.getSelectedRow();
//    if (!row) {
//        $.ligerDialog.success('请选择要编辑的行!'); return;
//    }
//    $.ligerDialog.open({ url: '../ProductIn/View?id=' + row["ID"].toString(), height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false
//    });
//}
//confirm
function confirmOperation() {
    var billId = $("#ID").val();
    if (billId != null && billId != undefined && $.trim(billId).length > 0) {
        $.ligerDialog.confirm('确定要提交吗？', function (yes) {
            if (yes) {
                if ($("#BillState").val() != "1") {
                    $.get("../ProductIn/Confirm?billId=" + billId, {}, function (data) {
                        if (data != undefined && data == "9") {
                            $.ligerDialog.success('提交成功!');
                            //initligerGrid();
                            ajaxSubmitSuccess(); //公共类：提交成功后关闭当前窗口
                        }
                        else if (data == "0") {
                            $.ligerDialog.success('请先保存后再提交!');
                        }
                        else {
                            $.ligerDialog.success('系统异常!');
                        }
                    });
                } else {
                    $.ligerDialog.success('已提交，不能重复提交!');
                }
            }
        });
    }
    else {
        $.ligerDialog.success('请先保存后再提交!');
    }
}
//=========================================
function intiToobar() {
    $("#toptoolbar").ligerToolBar({ items: [
                { text: '入库单管理',  icon: 'add' }
            ]
    });
}

//==============================

function f_onCheckAllRow(checked) {
    for (var rowid in this.records) {
        if (checked)
            addCheckedCustomer(this.records[rowid]['CustomerID']);
        else
            removeCheckedCustomer(this.records[rowid]['CustomerID']);
    }
}

/*
该例子实现 表单分页多选
即利用onCheckRow将选中的行记忆下来，并利用isChecked将记忆下来的行初始化选中
*/

var checkedCustomer = [];
function findCheckedCustomer(CustomerID) {
    for (var i = 0; i < checkedCustomer.length; i++) {
        if (checkedCustomer[i] == CustomerID) return i;
    }
    return -1;
}
function addCheckedCustomer(CustomerID) {
    if (findCheckedCustomer(CustomerID) == -1)
        checkedCustomer.push(CustomerID);
}
function removeCheckedCustomer(CustomerID) {
    var i = findCheckedCustomer(CustomerID);
    if (i == -1) return;
    checkedCustomer.splice(i, 1);
}
function f_isChecked(rowdata) {
    if (findCheckedCustomer(rowdata.CustomerID) == -1)
        return false;
    return true;
}
function f_onCheckRow(checked, data) {
    if (checked) addCheckedCustomer(data.ID);
    else removeCheckedCustomer(data.ID);
}
function f_getChecked() {
    alert(checkedCustomer.join(','));
}
